//
//  AsyncImageView.m
//  Postcard
//
//  Created by markj on 2/18/09.
//  Copyright 2009 Mark Johnson. You have permission to copy parts of this code into your own projects for any use.
//  www.markj.net
//

#import "AsyncImageView.h"
#import "ASIHTTPRequest.h"
#import <QuartzCore/QuartzCore.h>

@implementation AsyncImageView
@synthesize _imageRequestURL;
@synthesize _numStateRequested;
@synthesize m_BoolSetImage;

-(id)initWithFrame:(CGRect)frame{
	if ((self =[super initWithFrame:frame])) {
		m_BoolSetImage = NO;
        p_ActLoadView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        p_ActLoadView.frame = CGRectMake(frame.size.width/2-10, frame.size.height/2-10, 20, 20);
        
       
        [self addSubview:p_ActLoadView];
	}
	return self;
}

- (void)loadImageFromURL{
    NSURL* tRequestURL =nil;
    if ([_imageRequestURL isKindOfClass:[NSString class]]) {
        tRequestURL =[NSURL URLWithString:_imageRequestURL];
    }else{
        tRequestURL =_imageRequestURL;
    }
	_imageASIHttpRequest = [ASIHTTPRequest requestWithURL:tRequestURL];
    [_imageASIHttpRequest setTimeOutSeconds:20.f];
	[_imageASIHttpRequest setDelegate:self];
	[_imageASIHttpRequest setDidFinishSelector:@selector(okimg:)];
	[_imageASIHttpRequest setDidFailSelector:@selector(failimg:)];
	_numStateRequested = haveSend_Request;
    [_imageASIHttpRequest startAsynchronous];
    [_imageASIHttpRequest retain];
}

- (void)customLoadImageFromURL{
    [p_ActLoadView performSelectorOnMainThread:@selector(startAnimating) withObject:nil waitUntilDone:NO];
    [self loadImageFromURL];
}


-(void)okimg:(ASIHTTPRequest*) request{
	NSMutableData* imgdata = (NSMutableData*)[request responseData];
	if (p_ImageBuffer != nil) {
		[p_ImageBuffer release];
		p_ImageBuffer=nil;
	}
	p_ImageBuffer =  [UIImage imageWithData:imgdata];
	[p_ImageBuffer retain];

	_numStateRequested = suc_Request;
    
    [p_ActLoadView stopAnimating];
    [p_ActLoadView removeFromSuperview];
    [p_ActLoadView release];
    p_ActLoadView =nil;
    
	if (m_BoolSetImage) {
		[self initSelfImgView:p_ImageBuffer];
	}
    [_imageASIHttpRequest release];
    _imageASIHttpRequest = nil;
}

-(void) initSelfImgView :(UIImage*)img{
	if (_showImageView != nil) {
		[_showImageView removeFromSuperview];
		[_showImageView release];
		_showImageView = nil;
	}
	if (p_ImageBuffer != nil && _numStateRequested == suc_Request) {
		_showImageView = [[UIImageView alloc] initWithFrame:self.frame];
        [_showImageView setImage:p_ImageBuffer];
		[p_ImageBuffer release];
		p_ImageBuffer = nil;
		_numStateRequested = done_Image;
	}else {
        _showImageView = [[UIImageView alloc] initWithFrame:self.frame];
        [_showImageView setImage:img];
	}

	CGRect aframe = self.bounds;
	_showImageView.frame =aframe;
	self.contentMode = UIViewContentModeScaleAspectFit;
	[self addSubview:_showImageView];
}


-(void)failimg:(ASIHTTPRequest*) request{
	//获取图片失败
	NSLog(@"http jpeg error");
	_numStateRequested = fail_Request;
    [p_ActLoadView stopAnimating];
    [p_ActLoadView removeFromSuperview];
    [p_ActLoadView release];
    p_ActLoadView =nil;
    
    [_imageASIHttpRequest release];
    _imageASIHttpRequest = nil;
}

-(UIImage*) GetUIImage{
	for (id object in [self subviews]) {
		if ([object isKindOfClass:[UIImageView class]]) {
			return ((UIImageView*)object).image;
		}
	}
	return nil;
}

-(void) SetUIImage :(UIImage*)img{
	BOOL found = NO;
	for (id object in [self subviews]) {
		if ([object isKindOfClass:[UIImageView class]]) {
			((UIImageView*)object).image = img;
			found = YES;
		}
	}
	if (!found) {
		[self initSelfImgView:img];
	}
}

-(UIImage*) GetBufferImage{
	return p_ImageBuffer;
}

- (void)dealloc {
    if (_imageASIHttpRequest != nil && _numStateRequested == haveSend_Request) {
		[_imageASIHttpRequest setDelegate:nil];
		[_imageASIHttpRequest setDidFinishSelector:nil];
		[_imageASIHttpRequest setDidFailSelector:nil];
		[_imageASIHttpRequest cancel];
    }
    [_imageASIHttpRequest release];
    _imageASIHttpRequest = nil;
	if (_showImageView != nil) {
		[_showImageView removeFromSuperview];
		[_showImageView release];
	}
	if (p_ImageBuffer != nil) {
		[p_ImageBuffer release];
		p_ImageBuffer = nil;
	}
    [p_ActLoadView removeFromSuperview];
    [p_ActLoadView release];
    p_ActLoadView =nil;
    [_imageRequestURL release];
    [super dealloc];
}

@end
